package edu.gmu.swe632.fruit.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import edu.gmu.swe632.fruit.R;
import edu.gmu.swe632.fruit.service.GestureRecognizerService;
import event.GestureEvent;

public class RoadBlockedDistanceActivity extends DistanceActivity
{
	private static final String TAG = "roadblkdist";
	private static final Class<?> nextIntentClasses[] = { RoadBlockedDirectionActivity.class };

	private Bundle bundle;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		TextView title = (TextView) findViewById(R.id.title);
		title.setText("How far (ft)?");

		bundle = getIntent().getExtras();
	}

	@Override
	public void onResume()
	{
		super.onResume();

		Log.d(TAG, "onResume");
		GestureRecognizerService.addGestureListener(this);
	}

	@Override
	public void onPause()
	{
		super.onPause();

		Log.d(TAG, "onPause");
		GestureRecognizerService.removeGestureListener(this);
	}

	@Override
	public void onListItemClick(ListView parent, View v, int position, long id)
	{
		startNextActivity(position);
	}

	@Override
	public void gestureReceived(GestureEvent event)
	{
		if (event.getProbability() > 0.9)
		{
			String gesture = GestureRecognizerService.GestureIdMapping.get(event.getId());

			Log.d(TAG, "Gesture received " + gesture);

			if (gesture.compareTo(GestureRecognizerService.CHECK_GESTURE) == 0)
				startNextActivity(0);
			else if (gesture.compareTo(GestureRecognizerService.SOS_GESTURE) == 0)
				startActivity(new Intent(this, SosActivity.class));
		}
	}

	private void startNextActivity(int index)
	{
		if (0 <= index && index < nextIntentClasses.length)
		{
			int value = hundreds.getCurrentItem() * 100 + tens.getCurrentItem() * 10
					+ ones.getCurrentItem();

			bundle.putString(FruitMainActivity.RDBLOCK_DIST, String.valueOf(value) + " ft");

			Intent intent = new Intent(this, nextIntentClasses[index]).putExtras(bundle);
			startActivity(intent);
		}
	}
}
